<template>
  <div></div>
</template>

<script setup lang="ts">
import { pathResolve } from '@/utils'

defineOptions({ name: 'Redirect' })

/*
  router.replace({name: 'Redirect', params: {_redirect_type: 'path', path: '/system/user/list'}, query: {}})
  router.replace({name: 'Redirect', params: {_redirect_type: 'name', path: 'UserList'}, query: {}})
 */

const { currentRoute, replace } = useRouter()
// unref用于获取ref.value，但比后者更安全，如果参数是普通值，就原值返回，不会报错
const { params, query } = unref(currentRoute)
const { path, _redirect_type = 'path' } = params

Reflect.deleteProperty(params, '_redirect_type')
Reflect.deleteProperty(params, 'path')

const _path = Array.isArray(path) ? path.join('/') : path

if (_redirect_type === 'name') {
  replace({
    name: _path,
    params,
    query
  })
} else {
  replace({
    path: pathResolve('/', _path),
    query
  })
}
</script>

<style scoped lang="scss"></style>
